package com.zzy.portalssm.controller;

import com.zzy.portalssm.entity.Role;
import com.zzy.portalssm.entity.User;
import com.zzy.portalssm.service.RoleService;
import com.zzy.portalssm.service.UserService;
import com.zzy.portalssm.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"permission:query"})
    public String userList(@RequestParam(value = "username", defaultValue = "") String username, Model model) {
        model.addAttribute("username", username);
        return "system/user/list";
    }


    @RequestMapping("/list/page")
    @RequiresPermissions("permission:query")
    @ResponseBody
    public Result getUserListForPage(
            @RequestParam(value = "pno", defaultValue = "1") int pno,
            @RequestParam(value = "psize", defaultValue = "10") int psize,
            @RequestParam(value = "username", defaultValue = "") String username,
            @RequestParam(value = "sortField", defaultValue = "") String sortField,
            @RequestParam(value = "sortType", defaultValue = "") String sortType

    ) {
        return userService.getListForPage(pno, psize, username, sortField, sortType);
    }


    @RequestMapping("/add/page")
    public String userAddPage(Model model) {

        List<Role> roleList = roleService.selectRoleListAll();
        model.addAttribute("roleList", roleList);
        return "system/user/add";
    }

    @RequestMapping("/edit/page")
    public String userEditPage(Long id, Model model) {
        User user = userService.findUserById(id);
        List<Role> roleList = roleService.selectRoleListAll();
        model.addAttribute("formData", user);
        model.addAttribute("roleList", roleList);
        return "system/user/edit";
    }

    @RequestMapping("/add")
    public String userAdd(User user) {
        System.out.println(user);
        userService.insertUser(user);
        return "redirect:/user/list";
    }

    @RequestMapping("/edit")
    public String userEdit(User user) {
        System.out.println(user);
        userService.editUser(user);
        return "redirect:/user/list";
    }

    @RequestMapping("/delete")
    public String userDelete(Long id) {
        userService.deleteUserById(id);
        return "redirect:/user/list";
    }

    @RequestMapping("/password/page")
    public String userPasswordPage(HttpSession session, Model model) {
        User user = (User) session.getAttribute("userInfo");
        user.setPassword("");
        model.addAttribute("formData", user);
        return "system/password/password";
    }

    @RequestMapping("/password/change")
    public String userPasswordChange(HttpSession session, Model model, User user) {
        return userService.changePassword(session, user, model);
    }

    //跳转到部⻔员⼯⻚⾯
    @RequestMapping("/dept/list")
    @RequiresPermissions(value = {"permission:query"})
    public String userDeptList() {
        return "staff/user/list";
    }

    @RequestMapping("/dept/page")
    public String userDeptPage(Long id, Model model) {
        User user = userService.findUserById(id);
        //这个函数的⽬的是根据当前⽤户的部⻔信息直接⽣成⻚⾯对应的select标签组并且返回到⻚⾯
        String str = userService.getCheckedDeptList(user);
        model.addAttribute("select", str.trim());
        model.addAttribute("formData", user);
        return "staff/user/bind-dept";
    }

    @RequestMapping("/bind/dept")
    public String userBindDept(Long id, Long deptId) {
        userService.bindDept(id, deptId);
        return "redirect:/user/dept/list";
    }

}
